global d_user "Your directory" //Set the directory :)
set more off

********************************************************************************
// Evaluation of Delivering Breakfast After the Bell and Academic Performance //
// among Third-Grade Children: An Application of The Synthetic Control Method //
//                         Journal of School Health                           //
//                  Cuadros-Menaca, Thomsen, and Nayga (2022)                 //
********************************************************************************

//Estimate the SCM for each school receiving BAB

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==104021
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2012) ZELA(2014), trunit(104021) trperiod(2015) fig nested keep(resout104021, replace)
graph save Graph "$d_user\School104021.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405034
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2010) ZELA(2014), trunit(405034) trperiod(2015) fig nested keep(resout405034, replace)
graph save Graph "$d_user\School405034.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405036
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2010) ZELA(2012) ZELA(2014), trunit(405036) trperiod(2015) nested fig keep(resout405036, replace)
graph save Graph "$d_user\School405036.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405040
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2012) ZELA(2014), trunit(405040) trperiod(2015) fig nested keep(resout405040, replace)
graph save Graph "$d_user\School405040.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405041
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2011) ZELA(2012) ZELA(2014), trunit(405041) trperiod(2015) fig nested keep(resout405041, replace)
graph save Graph "$d_user\School405041.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405043
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2011) ZELA(2013) ZELA(2014), trunit(405043) trperiod(2015) fig nested keep(resout405043, replace)
graph save Graph "$d_user\School405043.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405044
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2013) ZELA(2014), trunit(405044) trperiod(2015) fig nested keep(resout405044, replace)
graph save Graph "$d_user\School405044.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405049
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2012) ZELA(2014), trunit(405049) trperiod(2015) fig nested keep(resout405049, replace)
graph save Graph "$d_user\School405049.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405050
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2010) ZELA(2012) ZELA(2014), trunit(405050) trperiod(2015) fig nested keep(resout405050, replace)
graph save Graph "$d_user\School405050.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405051
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2013) ZELA(2014), trunit(405051) trperiod(2015) fig nested keep(resout405051, replace)
graph save Graph "$d_user\School405051.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==701001
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2009) ZELA(2012) ZELA(2014), trunit(701001) trperiod(2015) nested fig keep(resout701001, replace)
graph save Graph "$d_user\School701001.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==1803025
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2010) ZELA(2014), trunit(1803025) trperiod(2015) nested fig keep(resout1803025, replace)
graph save Graph "$d_user\School1803025.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==1803026
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2011) ZELA(2014), trunit(1803026) trperiod(2015) fig nested keep(resout1803026, replace)
graph save Graph "$d_user\School1803026.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==1803028
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2011) ZELA(2014), trunit(1803028) trperiod(2015) fig keep(resout1803028, replace)
graph save Graph "$d_user\School1803028.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==1803029
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2011) ZELA(2014), trunit(1803029) trperiod(2015) fig nested keep(resout1803029, replace)
graph save Graph "$d_user\School1803029.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==2303021
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2013) ZELA(2014), trunit(2303021) trperiod(2015) fig keep(resout2303021, replace)
graph save Graph "$d_user\School2303021.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==2703009
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2009) ZELA(2011) ZELA(2014), trunit(2703009) trperiod(2015) fig nested keep(resout2703009, replace)
graph save Graph "$d_user\School2703009.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==3211022
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2009) ZELA(2011) ZELA(2014), trunit(3211022) trperiod(2015) fig nested keep(resout3211022, replace)
graph save Graph "$d_user\School3211022.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==3809014
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2010) ZELA(2013) ZELA(2014), trunit(3809014) trperiod(2015) fig nested keep(resout3809014, replace)
graph save Graph "$d_user\School3809014.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==3810026
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2011) ZELA(2012) ZELA(2014), trunit(3810026) trperiod(2015) fig nested keep(resout3810026, replace)
graph save Graph "$d_user\School3810026.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==4201001
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2010) ZELA(2013) ZELA(2014), trunit(4201001) trperiod(2015) fig nested keep(resout4201001, replace)
graph save Graph "$d_user\School4201001.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==4712043
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2012) ZELA(2013) ZELA(2014), trunit(4712043) trperiod(2015) fig nested keep(resout4712043, replace)
graph save Graph "$d_user\School4712043.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==7203027
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2012) ZELA(2014), trunit(7203027) trperiod(2015) fig nested keep(resout7203027, replace)
graph save Graph "$d_user\School7203027.gph", replace

//Graph each SC for all schools

graph combine "$d_user\School104021.gph" "$d_user\School405034.gph" /// 
              "$d_user\School405036.gph" "$d_user\School405040.gph" ///
			  "$d_user\School405041.gph" "$d_user\School405043.gph" ///
              "$d_user\School405044.gph" "$d_user\School405049.gph" ///
			  "$d_user\School405050.gph" "$d_user\School405051.gph" ///
              "$d_user\School701001.gph" "$d_user\School1803025.gph" ///
			  "$d_user\School1803026.gph" "$d_user\School1803028.gph" ///
              "$d_user\School1803029.gph" "$d_user\School2303021.gph" ///
			  "$d_user\School2703009.gph" "$d_user\School3211022.gph" ///
              "$d_user\School3809014.gph" "$d_user\School3810026.gph" ///
			  "$d_user\School4201001.gph" "$d_user\School4712043.gph" ///
              "$d_user\School7203027.gph" 
			  
graph save Graph "$d_user\Figure 2.gph", replace

//Compute the effects for all the schools 

*compute the difference between the real and the SC

foreach i in 104021 405034 405036 405040 405041 405043 405044 405049 405050 405051 ///
             701001 1803025 1803026 1803028 1803029 2303021 2703009 3211022 3809014 ///
			 3810026 4201001 4712043 7203027 {
use "$d_user\resout`i'.dta", clear
keep _Y_treated _Y_synthetic _time
drop if _time>2019.
rename _Y_treated _Y_treated`i'
rename _Y_synthetic _Y_synthetic`i'

gen a =  _Y_treated`i'> _Y_synthetic`i'
gen treated_positive = _Y_treated`i'>0
gen treated_negative = _Y_treated`i'<0
gen synthetic_positive = _Y_synthetic`i' >0
gen synthetic_negative = _Y_synthetic`i' <0
gen diffa = _Y_treated`i'- _Y_synthetic`i' if a==1 & treated_positive==1 & synthetic_positive==1
gen diffb = _Y_treated`i'- _Y_synthetic`i' if a==1 & treated_positive==1 & synthetic_negative==1
gen diffc= abs(_Y_synthetic`i')-abs(_Y_treated`i') if a==1 & treated_negative==1 & synthetic_negative==1
gen diffd= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_positive==1 & synthetic_positive==1 
gen diffe= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_negative==1 & synthetic_positive==1 
gen difff= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_negative==1 & synthetic_negative==1
egen effect`i' = rowtotal(diffa diffb diffc diffd diffe difff )
save "$d_user\SCM`i'.dta", replace
}

*Put together and compute the IATE

use "$d_user\SCM104021.dta", clear 
save "$d_user\SC_effects.dta", replace

foreach i in 405034 405036 405040 405041 405043 405044 405049 405050 405051 ///
             701001 1803025 1803026 1803028 1803029 2303021 2703009 3211022 3809014 ///
			 3810026 4201001 4712043 7203027 {
use "$d_user\SC_effects.dta", clear
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\SC_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   
   
global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\ELA_effects.dta", replace

//Compute the SC Effects For those schools adopting BIC 

use "$d_user\SCM104021.dta", clear 
save "$d_user\BIC_effects.dta", replace

foreach i in 405034 405036 405040 405041 405043 405044 405049 405050 405051 ///
             1803025 1803026 1803028 1803029 3810026{
use "$d_user\BIC_effects.dta", clear
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\BIC_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   

global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\BIC_effects.dta", replace

//Compute the SC Effects For those schools adopting G&G

use "$d_user\SCM2703009.dta", clear 
save "$d_user\GG_effects.dta", replace

foreach i in 3809014 4712043 3211022{
use "$d_user\GG_effects.dta", clear
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\GG_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   

global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\GG_effects.dta", replace

//Compute the SC Effects For those schools in small size districts

use "$d_user\SCM701001.dta", clear 
save "$d_user\Small_effects.dta", replace

foreach i in 2703009 3211022 3809014 7203027{
use "$d_user\Small_effects.dta", clear		
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\Small_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   
   
global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\Small_effects.dta", replace

//Compute the SC Effects For those schools in mid-size districts

use "$d_user\SCM104021.dta", clear 
save "$d_user\Mid_effects.dta", replace

foreach i in 3810026 4201001 4712043 2303021{
use "$d_user\Mid_effects.dta", clear		
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\Mid_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   
   
global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\Mid_effects.dta", replace

//Compute the SC Effects For those schools in big-size districts

use "$d_user\SCM1803025.dta", clear 
save "$d_user\Big_effects.dta", replace

foreach i in 1803026 1803028 1803029 2303021{
use "$d_user\Big_effects.dta", clear		
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\Big_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   
   
global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\Big_effects.dta", replace

//Compute the SC Effects For those schools in large-size districts

use "$d_user\SCM405034.dta", clear 
save "$d_user\Large_effects.dta", replace

foreach i in 405036 405040 405041 405043 405044 405049 405050 405051{
use "$d_user\Large_effects.dta", clear		
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\Large_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   
   
global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\Large_effects.dta", replace

//Compute P-values with all controls 

*Estimate the placebo SCM

foreach i in 201001 203027 401007 401009 401012 401014 401015 405033 405042	///
             405047	407026 440701 503011 503012	503013 505026 801004 803011	///
			 1201001 1203010 1304014 1305009 1408001 1408018 1503016 1505025 ///
			 1601001 1613010 1613031 1705020 1705022 1705029 1705030 1705032 ///
			 2203014 2301001 2301008 2301011 2304021 2306029 2402006 2502005 ///
			 2503009 2602005 2605033 2606042 2607046 2705023 2705024 2803016 ///
			 2807004 3102001 3301001 3302005 3509064 3510078 3510079 3510080 ///
			 3806018 4204016 4303012 4304001 4304002 4304007 4304008 4304013 ///
			 4304014 4401011 4603009 4605019 4605020 4605022 4701001 4708028 ///
			 4901001 5204025 5205028 5303010 5503010 5504014 5604015 5608035 ///
			 5801001 5804013 6001024 6001027 6001030 6001038 6001048 6003092 ///
			 6003093 6003095 6003104 6003108 6003110 6003112 6003113 6003129 ///
			 6003135 6003139 6003142 6003150 6043701 6302006 6302008 6302009 ///
			 6304029 6502005 6601001 6601002 6601003 6601006 6601007 6601008 ///
			 6601010 6601011 6601012 6601014 6601016 6601017 6601018 6601019 ///
			 6601029 6601030 6601031 6601032 6601033 6701001 6703012 6804009 ///
			 6901005 6901011 6901015 7003027 7007039 7008043 7202008 7202009 ///
			 7203012 7203013 7203016 7203017 7203022 7203023 7204027 7207044 ///
			 7207052 7207057 7207058 7207063 7207066 7208060 7301001 7307026 ///
			 7307030 7310042 7311046 7311047 7311053 7403012 7503005{
use "$d_user\Data.dta", clear
egen treated2 = total(BAB) if year>=2015, by (School)
egen unit_out2 = max(treated2), by (School)
drop if (unit_out2>0)
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2012) ZELA(2014), trunit(`i') trperiod(2015) keep(Placeboresout`i', replace)
}

foreach i in 201001 203027 401007 401009 401012 401014 401015 405033 405042	///
             405047	407026 440701 503011 503012	503013 505026 801004 803011	///
			 1201001 1203010 1304014 1305009 1408001 1408018 1503016 1505025 ///
			 1601001 1613010 1613031 1705020 1705022 1705029 1705030 1705032 ///
			 2203014 2301001 2301008 2301011 2304021 2306029 2402006 2502005 ///
			 2503009 2602005 2605033 2606042 2607046 2705023 2705024 2803016 ///
			 2807004 3102001 3301001 3302005 3509064 3510078 3510079 3510080 ///
			 3806018 4204016 4303012 4304001 4304002 4304007 4304008 4304013 ///
			 4304014 4401011 4603009 4605019 4605020 4605022 4701001 4708028 ///
			 4901001 5204025 5205028 5303010 5503010 5504014 5604015 5608035 ///
			 5801001 5804013 6001024 6001027 6001030 6001038 6001048 6003092 ///
			 6003093 6003095 6003104 6003108 6003110 6003112 6003113 6003129 ///
			 6003135 6003139 6003142 6003150 6043701 6302006 6302008 6302009 ///
			 6304029 6502005 6601001 6601002 6601003 6601006 6601007 6601008 ///
			 6601010 6601011 6601012 6601014 6601016 6601017 6601018 6601019 ///
			 6601029 6601030 6601031 6601032 6601033 6701001 6703012 6804009 ///
			 6901005 6901011 6901015 7003027 7007039 7008043 7202008 7202009 ///
			 7203012 7203013 7203016 7203017 7203022 7203023 7204027 7207044 ///
			 7207052 7207057 7207058 7207063 7207066 7208060 7301001 7307026 ///
			 7307030 7310042 7311046 7311047 7311053 7403012 7503005{
use "$d_user\Placeboresout`i'.dta", clear
keep _Y_treated _Y_synthetic _time
drop if _time>2019.
rename _Y_treated _Y_treated`i'
rename _Y_synthetic _Y_synthetic`i'

gen a =  _Y_treated`i'> _Y_synthetic`i'

gen treated_positive = _Y_treated`i'>0
gen treated_negative = _Y_treated`i'<0
gen synthetic_positive = _Y_synthetic`i' >0
gen synthetic_negative = _Y_synthetic`i' <0

gen diffa = _Y_treated`i'- _Y_synthetic`i' if a==1 & treated_positive==1 & synthetic_positive==1
gen diffb = _Y_treated`i'- _Y_synthetic`i' if a==1 & treated_positive==1 & synthetic_negative==1
gen diffc= abs(_Y_synthetic`i')-abs(_Y_treated`i') if a==1 & treated_negative==1 & synthetic_negative==1

gen diffd= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_positive==1 & synthetic_positive==1 
gen diffe= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_negative==1 & synthetic_positive==1 
gen difff= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_negative==1 & synthetic_negative==1

egen placebo`i' = rowtotal(diffa diffb diffc diffd diffe difff )

keep _time placebo`i'

save "$d_user\PlaceboSCM`i'.dta", replace
}

*Put the true effects and placebo effects together

use "$d_user\ELA_effects.dta", clear
save "$d_user\All_effects.dta", replace

foreach i in 201001 203027 401007 401009 401012 401014 401015 405033 405042	///
             405047	407026 440701 503011 503012	503013 505026 801004 803011	///
			 1201001 1203010 1304014 1305009 1408001 1408018 1503016 1505025 ///
			 1601001 1613010 1613031 1705020 1705022 1705029 1705030 1705032 ///
			 2203014 2301001 2301008 2301011 2304021 2306029 2402006 2502005 ///
			 2503009 2602005 2605033 2606042 2607046 2705023 2705024 2803016 ///
			 2807004 3102001 3301001 3302005 3509064 3510078 3510079 3510080 ///
			 3806018 4204016 4303012 4304001 4304002 4304007 4304008 4304013 ///
			 4304014 4401011 4603009 4605019 4605020 4605022 4701001 4708028 ///
			 4901001 5204025 5205028 5303010 5503010 5504014 5604015 5608035 ///
			 5801001 5804013 6001024 6001027 6001030 6001038 6001048 6003092 ///
			 6003093 6003095 6003104 6003108 6003110 6003112 6003113 6003129 ///
			 6003135 6003139 6003142 6003150 6043701 6302006 6302008 6302009 ///
			 6304029 6502005 6601001 6601002 6601003 6601006 6601007 6601008 ///
			 6601010 6601011 6601012 6601014 6601016 6601017 6601018 6601019 ///
			 6601029 6601030 6601031 6601032 6601033 6701001 6703012 6804009 ///
			 6901005 6901011 6901015 7003027 7007039 7008043 7202008 7202009 ///
			 7203012 7203013 7203016 7203017 7203022 7203023 7204027 7207044 ///
			 7207052 7207057 7207058 7207063 7207066 7208060 7301001 7307026 ///
			 7307030 7310042 7311046 7311047 7311053 7403012 7503005{
use "$d_user\All_effects", clear
merge 1:1 _time using "$d_user\PlaceboSCM`i'.dta"
drop _merge
keep _time effect placebo*
save "$d_user\All_effects.dta", replace
}

*Compute P-Value

use "$d_user\All_effects.dta", clear
drop if _time<2015
drop effect
reshape long placebo, i(_time) j(school) 
sort school _time

reshape wide placebo, i(school) j(_time)
drop school
save "$d_user\placebo.dta", replace

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2015
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 23 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

/* effect
use "$d_user\Breakfast_in_Classroom\Website\SC_effects.dta", clear
_time	effect
2015	-.04775421
2016	 .01797275
2017	-.09492047
2018	-.02315608
2019	-.00468124
*/

use "$d_user\p_values.dta", clear

histogram mean2015, tline(-.04775421) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2015 RMSPE All Units") xtitle("")
graph save Graph "$d_user\ELAAllPlacebo2015.gph", replace
histogram mean2016, tline(.01797275) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2016 RMSPE All Units") xtitle("")
graph save Graph "$d_user\ELAAllPlacebo2016.gph", replace
histogram mean2017, tline(-.09492047) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2017 RMSPE All Units") xtitle("")
graph save Graph "$d_user\ELAAllPlacebo2017.gph", replace
histogram mean2018, tline(-.02315608) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2018 RMSPE All Units") xtitle("")
graph save Graph "$d_user\ELAAllPlacebo2018.gph", replace
histogram mean2019, tline(-.00468124) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2019 RMSPE All Units") xtitle("")
graph save Graph "$d_user\ELAAllPlacebo2019.gph", replace

//Compute P-values baseline estimates all 

gen effect2015 = -.04775421
gen effect2016 =  .01797275
gen effect2017 = -.09492047
gen effect2018 = -.02315608
gen effect2019 = -.00468124
 
*two-sided p-values 
 
gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvalues_all.dta", replace

//Compute P-values BIC schools all

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2019 
return scalar mean = r(mean)
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019

set seed 1234
placebo2019
sum placebo2019

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.04784944
gen effect2016 =  .06805925
gen effect2017 = -.12253506
gen effect2018 = -.03840177
gen effect2019 = -.0588363

*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesBIC_all.dta", replace

//Compute P-values G&G with all schools

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 4 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.04379351
gen effect2016 = -.09201577
gen effect2017 = -.04123358
gen effect2018 = -.08832048
gen effect2019 =  .01162619

*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesGG_all.dta", replace

//Compute P-values Big District Enrollment with all schools

*Put them together

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.18277822
gen effect2016 = -.03453574
gen effect2017 = -.23733705
gen effect2018 = -.10183444
gen effect2019 = -.10641013
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesBigDistrict_all.dta", replace

//Compute P-values Small District Enrollment

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015

program placebo2016, rclass 
version 13 
drop _all
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 =  .01225618
gen effect2016 = -.10276178
gen effect2017 = -.1314791
gen effect2018 = -.10772727
gen effect2019 =  .0169757
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesSmallDistrict_all.dta", replace

//Compute P-values for schools in small medium districts

*Put them together

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.0694231
gen effect2016 =  .02878734
gen effect2017 =  .02065306
gen effect2018 =  .07382742
gen effect2019 =  .05186024
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesMidDistrict_all.dta", replace

//Compute P-values Large District Enrollment

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 9 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 9 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = .00525624
gen effect2016 = .12740377
gen effect2017 = .03710527
gen effect2018 = .11466749
gen effect2019 = .07691996
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesLargeDistrict_all.dta", replace

//Compute RMSPE by School in the control group

*Compute the Mean Squared Predictor Error for Placebo Estimates

use "$d_user\Data.dta", clear 
egen treated2 = total(BAB) if year>=2015, by (School)
egen unit_out2 = max(treated2), by (School)
drop if (unit_out2>0)

tsset School year	  

tempname resmat
foreach i in 201001 203027 401007 401009 401012 401014 401015 405033 405042	///
             405047	407026 440701 503011 503012	503013 505026 801004 803011	///
			 1201001 1203010 1304014 1305009 1408001 1408018 1503016 1505025 ///
			 1601001 1613010 1613031 1705020 1705022 1705029 1705030 1705032 ///
			 2203014 2301001 2301008 2301011 2304021 2306029 2402006 2502005 ///
			 2503009 2602005 2605033 2606042 2607046 2705023 2705024 2803016 ///
			 2807004 3102001 3301001 3302005 3509064 3510078 3510079 3510080 ///
			 3806018 4204016 4303012 4304001 4304002 4304007 4304008 4304013 ///
			 4304014 4401011 4603009 4605019 4605020 4605022 4701001 4708028 ///
			 4901001 5204025 5205028 5303010 5503010 5504014 5604015 5608035 ///
			 5801001 5804013 6001024 6001027 6001030 6001038 6001048 6003092 ///
			 6003093 6003095 6003104 6003108 6003110 6003112 6003113 6003129 ///
			 6003135 6003139 6003142 6003150 6043701 6302006 6302008 6302009 ///
			 6304029 6502005 6601001 6601002 6601003 6601006 6601007 6601008 ///
			 6601010 6601011 6601012 6601014 6601016 6601017 6601018 6601019 ///
			 6601029 6601030 6601031 6601032 6601033 6701001 6703012 6804009 ///
			 6901005 6901011 6901015 7003027 7007039 7008043 7202008 7202009 ///
			 7203012 7203013 7203016 7203017 7203022 7203023 7204027 7207044 ///
			 7207052 7207057 7207058 7207063 7207066 7208060 7301001 7307026 ///
			 7307030 7310042 7311046 7311047 7311053 7403012 7503005{
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZELA $controls ZELA ZELA(2012) ZELA(2014), trunit(`i') trperiod(2015)
matrix `resmat' = nullmat(`resmat')\e(RMSPE)
local names `"`names' `"`i'"'"'
}
mat colnames `resmat' = "RMSPE"
mat rownames `resmat' = `names'
matlist `resmat', row("Treated Unit") 

//Compute P-values with 75<RMSPE

*Put the true effects and placebo effects together

use "$d_user\ELA_effects.dta", clear
save "$d_user\All_effects.dta", replace

foreach i in 405047	1613010	6601002	2705024	407026 6601029 6302006 6001030 ///
             505026	2203014	5801001	2807004	2301011	6601012	6003092	5804013	///
			 4304007 4303012 503012	2502005	401015 4708028 7207058 401009 ///
			 7202009 6003139 6703012 2605033 2606042 7301001 6601006 7207057 ///
			 7311047 7202008 7207063 4304001 1705032 6001048 7203012 6302009 ///
			 6502005 6601033 401012	401007 3302005 6003142 7207066 6701001 ///
			 6003108 6601031 6003093 6601016 4304008 2602005 6601032 6001024 ///
			 6001027 7203016 6302008 7311053 2705023 7203022 4304014 503011 ///
			 6003135 4304013 3510080 1503016 4603009 801004	4605019	6003113	///
			 2402006 6601003 6003150 3806018 1705020 503013	3510078	803011 ///
			 6003129 6601017 6804009 6601018 5604015 1705029 7203013 405042	///
			 440701	2301008	6003095	1408001	3102001	7007039	1601001	6003112	///
			 6601001 405033	4605020	5204025	4901001	6003104	6601010	401014 ///
			 1613031 2306029 7208060 201001	5608035	2607046	6003110	6601007	///
			 6304029 4304002 7310042 7403012 7003027 6901005 7203017 203027	///
			 3509064{
use "$d_user\All_effects", clear
merge 1:1 _time using "$d_user\PlaceboSCM`i'.dta"
drop _merge
keep _time effect placebo*
save "$d_user\All_effects.dta", replace
}

*Compute P-Value

use "$d_user\All_effects.dta", clear
drop if _time<2015
drop effect
reshape long placebo, i(_time) j(school) 
sort school _time

reshape wide placebo, i(school) j(_time)
drop school
save "$d_user\placebo.dta", replace

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 23 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

//Compute P-values baseline estimates schools <75 RMSPE

/* effect
use "$d_user\Breakfast_in_Classroom\Website\SC_effects.dta", clear
_time	effect
2015	-.04775421
2016	 .01797275
2017	-.09492047
2018	-.02315608
2019	-.00468124
*/

use "$d_user\p_values.dta", clear

histogram mean2015, tline(-.04775421) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2015 RMSPE<75") xtitle("")
graph save Graph "$d_user\ELA75Placebo2015.gph", replace
histogram mean2016, tline(.01797275) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2016 RMSPE<75") xtitle("")
graph save Graph "$d_user\ELA75Placebo2016.gph", replace
histogram mean2017, tline(-.09492047) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2017 RMSPE<75") xtitle("")
graph save Graph "$d_user\ELA75Placebo2017.gph", replace
histogram mean2018, tline(-.02315608) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2018 RMSPE<75") xtitle("")
graph save Graph "$d_user\ELA75Placebo2018.gph", replace
histogram mean2019, tline(-.00468124) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2019 RMSPE<75") xtitle("")
graph save Graph "$d_user\ELA75Placebo2019.gph", replace

gen effect2015 = -.04775421
gen effect2016 =  .01797275
gen effect2017 = -.09492047
gen effect2018 = -.02315608
gen effect2019 = -.00468124

*two-sided p-values 
 
gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvalues_75.dta", replace

//Compute P-values with RMSPE less than 50th percentile 

*Put the true effects and placebo effects together

use "$d_user\ELA_effects.dta", clear
save "$d_user\All_effects.dta", replace

foreach i in 405047	1613010	6601002	2705024	407026 6601029 6302006 6001030 ///
             505026	2203014	5801001	2807004	2301011	6601012	6003092	5804013	///
			 4304007 4303012 503012	2502005	401015 4708028 7207058 401009 ///
			 7202009 6003139 6703012 2605033 2606042 7301001 6601006 7207057 ///
			 7311047 7202008 7207063 4304001 1705032 6001048 7203012 6302009 ///
			 6502005 6601033 401012	401007 3302005 6003142 7207066 6701001 ///
			 6003108 6601031 6003093 6601016 4304008 2602005 6601032 6001024 ///
			 6001027 7203016 6302008 7311053 2705023 7203022 4304014 503011 ///
			 6003135 4304013 3510080 1503016 4603009 801004	4605019	6003113	///
			 2402006 6601003 6003150 3806018 1705020 503013	3510078	803011 ///
			 6003129{
use "$d_user\All_effects", clear
merge 1:1 _time using "$d_user\PlaceboSCM`i'.dta"
drop _merge
keep _time effect placebo*
save "$d_user\All_effects.dta", replace
}

*Compute P-Value

use "$d_user\All_effects.dta", clear
drop if _time<2015
drop effect
reshape long placebo, i(_time) j(school) 
sort school _time

reshape wide placebo, i(school) j(_time)
drop school
save "$d_user\placebo.dta", replace

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

//Compute P-values baseline estimates schools <50 RMSPE

/* effect
use "$d_user\Breakfast_in_Classroom\Website\SC_effects.dta", clear
_time	effect
2015	-.04775421
2016	 .01797275
2017	-.09492047
2018	-.02315608
2019	-.00468124
*/

use "$d_user\p_values.dta", clear

histogram mean2015, tline(-.04775421) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2015 RMSPE<50") xtitle("")
graph save Graph "$d_user\ELA50Placebo2015.gph", replace
histogram mean2016, tline(.01797275) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2016 RMSPE<50") xtitle("")
graph save Graph "$d_user\ELA50Placebo2016.gph", replace
histogram mean2017, tline(-.09492047) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2017 RMSPE<50") xtitle("")
graph save Graph "$d_user\ELA50Placebo2017.gph", replace
histogram mean2018, tline(-.02315608) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2018 RMSPE<50") xtitle("")
graph save Graph "$d_user\ELA50Placebo2018.gph", replace
histogram mean2019, tline(-.00468124) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2019 RMSPE<50") xtitle("")
graph save Graph "$d_user\ELA50Placebo2019.gph", replace

gen effect2015 = -.04775421
gen effect2016 =  .01797275
gen effect2017 = -.09492047
gen effect2018 = -.02315608
gen effect2019 = -.00468124

*two-sided p-values 
 
gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvalues_50.dta", replace

//Compute P-values BIC schools RMSPE<50

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2018
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.04784944
gen effect2016 =  .06805925
gen effect2017 = -.12253506
gen effect2018 = -.03840177
gen effect2019 = -.0588363

*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesBIC_50.dta", replace

//Compute P-values G&G schools RMSPE<50

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4 
summarize placebo2015
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4 
summarize placebo2018
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.04379351
gen effect2016 = -.09201577
gen effect2017 = -.04123358
gen effect2018 = -.08832048
gen effect2019 =  .01162619

*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesGG_50.dta", replace

//Compute P-values Big District Enrollment

*Put them together

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.18277822
gen effect2016 = -.03453574
gen effect2017 = -.23733705
gen effect2018 = -.10183444
gen effect2019 = -.10641013
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesBigDistrict_50.dta", replace

//Compute P-values Small District Enrollment

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 =  .01225618
gen effect2016 = -.10276178
gen effect2017 = -.1314791
gen effect2018 = -.10772727
gen effect2019 =  .0169757
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesSmallDistrict_50.dta", replace

//Compute P-values for schools in small medium districts

*Put them together

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.0694231
gen effect2016 =  .02878734
gen effect2017 =  .02065306
gen effect2018 =  .07382742
gen effect2019 =  .05186024
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesMidDistrict_50.dta", replace

//Compute P-values Large District Enrollment

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = .00525624
gen effect2016 = .12740377
gen effect2017 = .03710527
gen effect2018 = .11466749
gen effect2019 = .07691996
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesLargeDistrict_50.dta", replace
